###############
# Master file
# Simulation
###############

# clear memory
rm(list=ls())

# set the working directory to the current file location!
# setwd()


# source function
source("simulation/cbq_binary_function.R")

# estimation
t0 <- Sys.time()
# homogeneously simulated data
source("simulation/sim_homo.R")

t1 <- Sys.time()
# heterogeneously generated data
source("simulation/sim_heter.R")

t2 <- Sys.time()

# table1
pred_rate_all <- cbind(pred_rate_homo,pred_rate_heter)
l1 <- length(which(pred_rate_homo[2:10] >= pred_rate_homo[1]))
l2 <- length(which(pred_rate_heter[2:10] >= pred_rate_heter[1]))
b1 <- paste(l1,"/9",sep="")
b2 <- paste(l2,"/9",sep="")
function_c <- function(x){
  paste0(sprintf("%.1f", x*100), "%")
}
pred_rate_all_c <- apply(pred_rate_all,1:2,function_c)
pred_rate_all_c <- rbind(pred_rate_all_c,c(b1,b2))
pred_rate_all_c <- as.data.frame(pred_rate_all_c)
row_name <- c("Ordinary logit",
              "CBQ-Q1",
              "CBQ-Q2",
              "CBQ-Q3",
              "CBQ-Q4",
              "CBQ-Q5",
              "CBQ-Q6",
              "CBQ-Q7",
              "CBQ-Q8",
              "CBQ-Q9",
              "Proportion of equal or better predictions"
              )
col_name <- c("Homogeneous data",
              "Heterogeneous data")
colnames(pred_rate_all_c) <- col_name
rownames(pred_rate_all_c) <- row_name
write.csv(pred_rate_all_c, 'tables/table1.csv')

t3 <- Sys.time()

dur1 <- t1 - t0 # sim_homo.R
dur2 <- t2 - t1 # sim_heter.R 
dur3 <- t3 - t2 # table
dur4 <- t3 - t0 # total
dur1 
dur2
dur3
dur4
